const api = require('../../utils/api.js');
const util = require('../../utils/util.js');

const formatTime = util.formatTime;

Page({
    data: {
		trips:[],
		start: 0,
		loading: 'block'
    },
    onLoad: function(options) {
		this.loadmore();
    },
    onPullDownRefresh: function() {
		this.loadmore();
		wx.stopPullDownRefresh();
    },
	onReachBottom:function(){
		this.setData({
			loading: 'block'
		})
		this.loadmore(null, true);
	},
	loadmore: function(e,needrefresh){
		let self = this;
		let data = {
			next_start: self.data.start
		} 
		api.trip.hot(data,(state,res)=>{
			if (state === 'success'){
				// console.log(res.data);
				let newList = res.data.data.elements;
				newList.map((trip) => {
					let item = trip;
					item.data[0].date_added = formatTime(new Date(item.data[0].date_added * 1000),1)
					return item;
				});
				if (needrefresh){
					newList = self.data.trips.concat(newList);
				};
				self.setData({
					trips: newList,
					start: res.data.data.next_start,
					loading: 'none'
				});
				// console.log(newList)
			}
		})
	},
	viewTrip:function(e){
		let dataTrip = e.currentTarget.dataset;
		wx.navigateTo({
			url: `../trip/trip?id=${dataTrip.id}&name=${dataTrip.name}`,
		})
	}
})
